Conditions | 1 |
Paths | 1 |
Total Lines | 207 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | var chai = require('chai'); |
||
21 | describe('cmsOperations', function() { |
||
22 | before( function(done) { |
||
23 | Manager.instance.init() |
||
24 | .then(function () { |
||
25 | Manager.instance._whereKeys = ['title', 'priority', 'abe_meta', 'articles'] |
||
26 | Manager.instance.updateList() |
||
27 | |||
28 | this.fixture = { |
||
29 | htmlArticle: fse.readFileSync(path.join(process.cwd(), 'test', 'fixtures', 'templates', 'article.html'), 'utf8'), |
||
30 | jsonArticle: fse.readJsonSync(path.join(process.cwd(), 'test', 'fixtures', 'files', 'article-2.json')), |
||
31 | jsonHomepage: fse.readJsonSync(path.join(process.cwd(), 'test', 'fixtures', 'data', 'homepage-1.json')) |
||
32 | } |
||
33 | done() |
||
34 | |||
35 | }.bind(this)) |
||
36 | }); |
||
37 | |||
38 | /** |
||
39 | * cmsOperations.post.publish |
||
40 | * |
||
41 | */ |
||
42 | it('cmsOperations.post.publish()', function(done) { |
||
43 | // stub |
||
44 | var s = sinon.sandbox.create(); |
||
45 | s.stub(abeExtend.hooks.instance, 'trigger', function (str, obj) { return str, obj; }.bind(this)); |
||
46 | s.stub(cmsTemplates.template, 'getTemplate', function () { return this.fixture.htmlArticle; }.bind(this)); |
||
47 | s.stub(cmsData.source, 'getDataList', function () { |
||
48 | return Promise.resolve(JSON.parse(JSON.stringify(this.fixture.jsonArticle))) |
||
49 | }.bind(this)); |
||
50 | s.stub(cmsData.utils, 'getPercentOfRequiredTagsFilled', function () { return 100; }.bind(this)); |
||
51 | // s.stub(Page, 'getPercentOfRequiredTagsFilled', function () { return 100; }.bind(this)); |
||
52 | s.stub(cmsOperations.save, 'saveHtml', function () { return 100; }.bind(this)); |
||
53 | s.stub(cmsOperations.save, 'saveJson', function () { return 100; }.bind(this)); |
||
54 | s.stub(Manager.instance, 'updatePostInList', function () { return null; }.bind(this)); |
||
55 | |||
56 | // test |
||
57 | cmsOperations.post.publish('article-2.html', JSON.parse(JSON.stringify(this.fixture.jsonArticle))) |
||
58 | .then(function(resSave) { |
||
59 | // unstub |
||
60 | abeExtend.hooks.instance.trigger.restore() |
||
61 | cmsTemplates.template.getTemplate.restore() |
||
62 | cmsData.source.getDataList.restore() |
||
63 | cmsData.utils.getPercentOfRequiredTagsFilled.restore() |
||
64 | cmsOperations.save.saveHtml.restore() |
||
65 | cmsOperations.save.saveJson.restore() |
||
66 | Manager.instance.updatePostInList.restore() |
||
67 | done() |
||
68 | }.bind(this)); |
||
69 | }); |
||
70 | |||
71 | /** |
||
72 | * cmsOperations.post.unpublish |
||
73 | * |
||
74 | */ |
||
75 | it('cmsOperations.post.unpublish()', function(done) { |
||
76 | // stub |
||
77 | var s = sinon.sandbox.create(); |
||
78 | s.stub(abeExtend.hooks.instance, 'trigger', function (str, obj) { return str, obj; }.bind(this)); |
||
79 | s.stub(coreUtils.file, 'exist', function (revisionPath) { return true; }.bind(this)); |
||
80 | s.stub(cmsData.file, 'get', function () { return JSON.parse(JSON.stringify(this.fixture.jsonArticle)); }.bind(this)); |
||
81 | s.stub(cmsOperations.post, 'draft', function () { |
||
82 | return Promise.resolve({json: JSON.parse(JSON.stringify(this.fixture.jsonArticle))}) |
||
83 | }.bind(this)); |
||
84 | s.stub(cmsOperations.remove, 'removeFile', function () { return null; }.bind(this)); |
||
85 | s.stub(Manager.instance, 'updatePostInList', function () { return null; }.bind(this)); |
||
86 | |||
87 | // test |
||
88 | cmsOperations.post.unpublish('article-2.html') |
||
89 | .then(function(resSave) { |
||
90 | |||
91 | // unstub |
||
92 | abeExtend.hooks.instance.trigger.restore() |
||
93 | coreUtils.file.exist.restore() |
||
94 | cmsData.file.get.restore() |
||
95 | cmsOperations.post.draft.restore() |
||
96 | cmsOperations.remove.removeFile.restore() |
||
97 | Manager.instance.updatePostInList.restore() |
||
98 | done() |
||
99 | }.bind(this)); |
||
100 | }); |
||
101 | |||
102 | /** |
||
103 | * cmsOperations.post.draft |
||
104 | * |
||
105 | */ |
||
106 | it('cmsOperations.post.draft()', function(done) { |
||
107 | var json = JSON.parse(JSON.stringify(this.fixture.jsonArticle)) |
||
108 | var meta = json.abe_meta |
||
109 | delete json.abe_meta |
||
110 | |||
111 | // stub |
||
112 | var s = sinon.sandbox.create(); |
||
113 | s.stub(abeExtend.hooks.instance, 'trigger', function (str, obj) { return str, obj; }.bind(this)); |
||
114 | s.stub(coreUtils.file, 'addDateIsoToRevisionPath', function (revisionPath) { return revisionPath; }.bind(this)); |
||
115 | s.stub(cmsData.metas, 'add', function (json) { |
||
116 | json.abe_meta = meta |
||
117 | return json; |
||
118 | }.bind(this)); |
||
119 | s.stub(cmsTemplates.template, 'getTemplate', function () { return this.fixture.htmlArticle; }.bind(this)); |
||
120 | s.stub(cmsData.source, 'getDataList', function () { |
||
121 | return Promise.resolve(JSON.parse(JSON.stringify(this.fixture.jsonArticle))) |
||
122 | }.bind(this)); |
||
123 | s.stub(cmsOperations.save, 'saveJson', function () { return true; }.bind(this)); |
||
124 | s.stub(Manager.instance, 'updatePostInList', function () { return null; }.bind(this)); |
||
125 | s.stub(cmsData.utils, 'getPercentOfRequiredTagsFilled', function () { return 100; }.bind(this)); |
||
126 | |||
127 | // test |
||
128 | cmsOperations.post.draft('article-2.html', JSON.parse(JSON.stringify(this.fixture.jsonArticle))) |
||
129 | .then(function(resSave) { |
||
130 | chai.expect(resSave.success).to.be.equal(1); |
||
131 | chai.expect(resSave.json.abe_meta).to.not.be.undefined; |
||
132 | |||
133 | // unstub |
||
134 | abeExtend.hooks.instance.trigger.restore() |
||
135 | coreUtils.file.addDateIsoToRevisionPath.restore() |
||
136 | cmsData.utils.getPercentOfRequiredTagsFilled.restore() |
||
137 | cmsData.metas.add.restore() |
||
138 | cmsTemplates.template.getTemplate.restore() |
||
139 | cmsData.source.getDataList.restore() |
||
140 | cmsOperations.save.saveJson.restore() |
||
141 | Manager.instance.updatePostInList.restore() |
||
142 | |||
143 | done() |
||
144 | }.bind(this)); |
||
145 | }); |
||
146 | |||
147 | /** |
||
148 | * cmsOperations.post.submit |
||
149 | * |
||
150 | */ |
||
151 | it('cmsOperations.post.submit()', function(done) { |
||
152 | // stub |
||
153 | var s = sinon.sandbox.create(); |
||
154 | s.stub(cmsOperations.post, 'edit', function (filePath, json, rejectToWorkflow) { |
||
155 | return Promise.resolve({ |
||
156 | success: 1, |
||
157 | json: this.fixture.jsonArticle |
||
158 | }); |
||
159 | }.bind(this)); |
||
160 | |||
161 | // test |
||
162 | var json = JSON.parse(JSON.stringify(this.fixture.jsonArticle)) |
||
163 | json.abe_meta.status = 'publish' |
||
164 | cmsOperations.post.submit('article-2.html', json) |
||
165 | .then(function(resSave) { |
||
166 | chai.expect(resSave.json.abe_meta).to.not.be.undefined; |
||
167 | |||
168 | // unstub |
||
169 | cmsOperations.post.edit.restore() |
||
170 | done() |
||
171 | }.bind(this)); |
||
172 | }); |
||
173 | |||
174 | /** |
||
175 | * cmsOperations.post.edit |
||
176 | * |
||
177 | */ |
||
178 | it('cmsOperations.post.edit()', function(done) { |
||
179 | // stub |
||
180 | var s = sinon.sandbox.create(); |
||
181 | s.stub(cmsOperations.post, 'draft', function (filePath, json, rejectToWorkflow) { |
||
182 | return Promise.resolve({ |
||
183 | success: 1, |
||
184 | json: this.fixture.jsonArticle |
||
185 | }); |
||
186 | }.bind(this)); |
||
187 | |||
188 | // test |
||
189 | var json = JSON.parse(JSON.stringify(this.fixture.jsonArticle)) |
||
190 | json.abe_meta.status = 'publish' |
||
191 | cmsOperations.post.edit('article-2.html', json) |
||
192 | .then(function(resSave) { |
||
193 | chai.expect(resSave.json.abe_meta).to.not.be.undefined; |
||
194 | |||
195 | // unstub |
||
196 | cmsOperations.post.draft.restore() |
||
197 | done() |
||
198 | }.bind(this)); |
||
199 | }); |
||
200 | |||
201 | /** |
||
202 | * cmsOperations.post.reject |
||
203 | * |
||
204 | */ |
||
205 | it('cmsOperations.post.reject()', function(done) { |
||
206 | // stub |
||
207 | var s = sinon.sandbox.create(); |
||
208 | s.stub(abeExtend.hooks.instance, 'trigger', function (str, obj) { return str, obj; }.bind(this)); |
||
209 | s.stub(cmsOperations.post, 'draft', function (filePath, json, rejectToWorkflow) { |
||
210 | chai.expect(rejectToWorkflow).to.be.equal("draft"); |
||
211 | return Promise.resolve(this.fixture.jsonArticle); |
||
212 | }.bind(this)); |
||
213 | |||
214 | // test |
||
215 | var json = JSON.parse(JSON.stringify(this.fixture.jsonArticle)) |
||
216 | json.abe_meta.status = 'publish' |
||
217 | cmsOperations.post.reject('article-2.html', json) |
||
218 | .then(function(resSave) { |
||
219 | chai.expect(resSave.abe_meta).to.not.be.undefined; |
||
220 | |||
221 | // unstub |
||
222 | abeExtend.hooks.instance.trigger.restore() |
||
223 | cmsOperations.post.draft.restore() |
||
224 | done() |
||
225 | }.bind(this)); |
||
226 | }); |
||
227 | }); |
||
228 |
This check looks for variables that are declared in multiple lines. There may be several reasons for this.
In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.
If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.